package com.keke.myblogv1.front.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.keke.myblogv1.article.dto.QueryArticleByPageDTO;
import com.keke.myblogv1.article.entity.MyBlogArticle;
import com.keke.myblogv1.article.service.MyBlogArticleService;
import com.keke.myblogv1.common.result.ResponseResult;
import com.keke.myblogv1.girlfriend.dto.QueryMyBlogGirlfriendByPageDTO;
import com.keke.myblogv1.girlfriend.entity.MyBlogGirlfriend;
import com.keke.myblogv1.girlfriend.service.MyBlogGirlfriendService;
import com.keke.myblogv1.updatelog.dto.QueryUpdateLogByPageDTO;
import com.keke.myblogv1.updatelog.entity.MyBlogUpdateLog;
import com.keke.myblogv1.updatelog.service.MyBlogUpdateLogService;
import com.keke.myblogv1.usermessage.dto.QueryUserMessageDTO;
import com.keke.myblogv1.usermessage.entity.MyBlogUserMessage;
import com.keke.myblogv1.usermessage.service.MyBlogUserMessageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Derrick
 * @since 2022-07-31
 */
@RestController
@RequestMapping("/front")
public class FrontController {

    @Resource
    private MyBlogArticleService myBlogArticleService;

    @Resource
    private MyBlogGirlfriendService myBlogGirlfriendService;

    @Resource
    private MyBlogUpdateLogService myBlogUpdateLogService;

    @Resource
    private MyBlogUserMessageService myBlogUserMessageService;


    @RequestMapping(value = "/article", method = RequestMethod.GET)
    public ResponseResult<Page<MyBlogArticle>> getArticleList(QueryArticleByPageDTO queryArticleByPageDTO) {
        Page<MyBlogArticle> allArticleByPage = null;
        try {
            allArticleByPage = myBlogArticleService.getAllArticleByPage(queryArticleByPageDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error();
        }
        return ResponseResult.success(allArticleByPage);
    }

    @RequestMapping(value = "/article/{id}", method = RequestMethod.GET)
    public ResponseResult<MyBlogArticle> getArticleById(@PathVariable("id") int id) {
        MyBlogArticle allArticleById = null;
        try {
            allArticleById = myBlogArticleService.getAllArticleById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error();
        }
        return ResponseResult.success(allArticleById);
    }

    @RequestMapping(value = "/girlfriend/list", method = RequestMethod.GET)
    public ResponseResult<List<MyBlogGirlfriend>> getGirlfriendList() {
        List<MyBlogGirlfriend> girlfriendList = null;
        try {
            girlfriendList = myBlogGirlfriendService.getGirlfriendList();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error();
        }
        return ResponseResult.success(girlfriendList);
    }

    @RequestMapping(value = "/girlfriend",method = RequestMethod.GET)
    public ResponseResult<Page<MyBlogGirlfriend>> getGirlfriendListByPage(QueryMyBlogGirlfriendByPageDTO queryMyBlogGirlfriendByPageDTO){
        Page<MyBlogGirlfriend> girlfriendListByPage = null;
        try {
            girlfriendListByPage = myBlogGirlfriendService.getGirlfriendListByPage(queryMyBlogGirlfriendByPageDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error();
        }
        return ResponseResult.success(girlfriendListByPage);
    }

    @RequestMapping(value = "/updatelog/list",method = RequestMethod.GET)
    public ResponseResult<List<MyBlogUpdateLog>> getMyBlogUpdateLogList(){
        List<MyBlogUpdateLog> myBlogUpdateLogList = null;
        try{
            myBlogUpdateLogList = myBlogUpdateLogService.getMyBlogUpdateLogList();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error();
        }
        return ResponseResult.success(myBlogUpdateLogList);
    }

    @RequestMapping(value = "/updatelog/listByPage",method = RequestMethod.GET)
    public ResponseResult<Page<MyBlogUpdateLog>> getMyBlogUpdateLogListByPage(QueryUpdateLogByPageDTO queryUpdateLogByPageDTO){
        Page<MyBlogUpdateLog> myBlogUpdateLogList = null;
        try{
            myBlogUpdateLogList = myBlogUpdateLogService.getMyBlogUpdateLogListByPage(queryUpdateLogByPageDTO);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error();
        }
        return ResponseResult.success(myBlogUpdateLogList);
    }

    @RequestMapping(value = "/usermessage/list",method = RequestMethod.GET)
    public ResponseResult<List<MyBlogUserMessage>> getMyBlogUserMessageList(){
        List<MyBlogUserMessage> myBlogUserMessageList=null;
        try{
            myBlogUserMessageList = myBlogUserMessageService.getMyBlogUserMessageList();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error();
        }
        return ResponseResult.success(myBlogUserMessageList);
    }
    @RequestMapping(value = "/usermessage/listByPage",method = RequestMethod.GET)
    public ResponseResult<Page<MyBlogUserMessage>> getMyBlogUserMessageListByPage(QueryUserMessageDTO queryUserMessageDTO){
        Page<MyBlogUserMessage> myBlogUserMessageList=null;
        try{
            myBlogUserMessageList = myBlogUserMessageService.getMyBlogUserMessageListByPage(queryUserMessageDTO);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error();
        }
        return ResponseResult.success(myBlogUserMessageList);
    }

    @RequestMapping(value = "/usermessage/insert",method = RequestMethod.POST)
    public ResponseResult insertMessage(@RequestBody MyBlogUserMessage myBlogUserMessage){
        boolean result = false;
        try{
            result = myBlogUserMessageService.insertMessage(myBlogUserMessage);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error();
        }
        if(result){
            return ResponseResult.success();
        }else{
            return ResponseResult.error();
        }
    }
}
